package dawnland03.model.action.impl;

import dawnland03.model.action.ActionImpl;
import dawnland03.model.action.ActionType;
import dawnland03.model.action.handler.ActionHandler;
import dawnland03.model.action.parameter.ActionInputParam;
import dawnland03.model.action.parameter.ActionOutputParam;
import dawnland03.model.entity.Entity;
import dawnland03.model.entity.attribute.PhysicalAttribute;
import dawnland03.service.GeneralFacade;

import java.util.Map;

/**
 * User: Petru Obreja (obrejap@yahoo.com)
 * Date: Jun 2, 2010
 * Time: 9:15:07 PM
 */
public class AcquireLockAction extends ActionImpl {
    AcquireLockAction(Entity owner, Map<ActionInputParam, Object> actionInputParams,
                      Map<PhysicalAttribute, Integer> physicalAttributeRequirements,
                      Map<PhysicalAttribute, Double> physicalAttributeMultiplier,
                      long duration, ActionHandler actionHandler) {
        super(owner, ActionType.ACQUIRE_LOCK, actionInputParams,
                physicalAttributeRequirements, physicalAttributeMultiplier, duration, actionHandler);
    }

    @Override
    protected void endAction() {
        Integer destionationMatrixCellId = (Integer) actionInputParams.get(ActionInputParam.DESTINATION);
        Long timeout = (Long) actionInputParams.get(ActionInputParam.TIMEOUT);
        boolean lockAcquired = GeneralFacade.acquireLockOnEntityLayer(owner.getId(), destionationMatrixCellId, timeout);
        outputParameters.addOutputParamValue(ActionOutputParam.DESTINATION, destionationMatrixCellId);
        outputParameters.addOutputParamValue(ActionOutputParam.COMPLETED, lockAcquired);
        super.endAction();
    }
}
